package model;

import java.util.ArrayList;

/**
 *
 * @author Martin Mohr Pedersen
 */
public class Sale {

    public static final String STATUS_OPEN = "open";
    public static final String STATUS_CLOSED = "closed";
    public static final String STATUS_CANCELLED = "cancelled";
    private ArrayList<SaleLineItem> lineItems;
    private Payment payment;
    private Customer customer;
    private double salePrice;
    private String status;
    private Delivery delivery;
    private double discount;

    public Sale() {
        lineItems = new ArrayList<SaleLineItem>();
        status = STATUS_OPEN;
    }

    public void addItem(Item item, int qty) {

        if (containsItem(item)) { // check if item is already added
            SaleLineItem line = getLineForItem(item);
            qty += line.getQty(); // add to new qty
            line.setQty(qty);
        } else {
            SaleLineItem line = new SaleLineItem(item, qty);
            lineItems.add(line);
        }
    }

    /**
     * Returns the SaleLineItem at given index
     * @param index
     * @return SaleLineItem at given index
     */
    public SaleLineItem getLine(int index) {
        return lineItems.get(index);
    }

    public void removeLine(SaleLineItem line) {
        lineItems.remove(line);
    }

    /**
     * Checks if sale contains specified item
     *
     * @param item
     *            Item to check for
     * @return True if sale contains item, false otherwise
     */
    public boolean containsItem(Item item) {
        SaleLineItem line = getLineForItem(item);
        return line != null;
    }

    /**
     * Fetches SaleLineItem object for specified item
     *
     * @param item
     *            Item to search for
     * @return SaleLineItem containing item, null if no such item
     */
    public SaleLineItem getLineForItem(Item item) {
        for (SaleLineItem line : lineItems) {
            if (line.getItem() == item) {
                return line;
            }
        }

        return null;
    }

    public void addItem(String itemNo, int qty) {

        Item item = ItemCollection.getInstance().getItem(itemNo);
        if (item != null) {
            addItem(item, qty);
        }

    }

    /**
     * Calculates total value of the sale
     *
     * @return total value of sale
     */
    public double getTotal() {

        double total = 0;

        total += getSubTotal();

        double saving = getSavings();

        total -= saving;

        if (getDelivery() != null) {
            total += getDelivery().getPrice();
        }

        return total;

    }

    public double getSubTotal() {
        double total = 0;

        for (SaleLineItem lineItem : lineItems) {
            total += (lineItem.getItem().getTotalPrice() * lineItem.getQty());
        }

        return total;
    }

    public void registerCashPayment(double amount) {
        payment = new Payment(amount);
    }

    public void close() {
        this.status = STATUS_CLOSED;
        this.salePrice = getTotal();
    }

    public SaleLineItem[] getLineItems() {
        return lineItems.toArray(new SaleLineItem[lineItems.size()]);
    }

    public void cancelSale() {
        status = STATUS_CANCELLED;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public double getSalePrice() {
        return this.salePrice;
    }

    public Customer getCustomer() {
        return customer;
    }

    public Payment getPayment() {
        return payment;
    }

    public void setDelivery(Delivery delivery) {
        this.delivery = delivery;
    }

    public Delivery getDelivery() {
        return delivery;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }

    public double getSavings() {
        return (discount / 100) * getSubTotal();
    }

    public void clearCustomer() {
        this.customer = null;
    }

    public void clearDelivery() {
        this.delivery = null;
    }
}
